-- CloudsShrine
-- Created by chengb Jul/28/2016
-- 云雨祠堂格子

-- 初始化
function initCloudsShrine(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            local dbase = DungeonM.queryElement(self.gridData.element, "dbase");
            if not BraveVSDragonM.isBuidingUnlock(dbase["lock_id"]) then
                -- 建筑未解锁
                alert(getLocStr("cannot_arrive"));
                return;
            end

            -- 如果已经加入龙王，提示被毁灭
            local choose = BraveVSDragonM.getDragonKingJoinChoose();
            if choose == BraveVSDragonM.CHOOSE_TYPE_ACCEPT then
                alert(getLocStr("dragon_king_destory"));
                return;
            end

            if BraveVSDragonM.hasGotBuildingBonus(self.gridData.class) then
                -- 已经领取过奖励
                alert(getLocStr("clouds_rain_shrine_tip"));
                return;
            end

            local function okFunc()
                local cost = FormulaM.invoke("CALC_CLOUDS_SHRINE_COST");
                if type(cost) == "table" and #cost > 0 then
                    local classId = cost[2];
                    local amount = cost[3];
                    local curAmount = ItemM.getAmount(ME.user, classId);
                    if curAmount < amount then
                        -- 缺少所需的物品
                        alert(getLocStr("lack_submit_item"));
                        return;
                    end
                end
                DungeonActionM.go("brave_building_bonus", self.gridData:getPos());
            end
            self:openBuildingView(okFunc);
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
end

